#include"list.h"
#include<stdio.h>



c_list list_init(void)
{
    c_list head=calloc(1,sizeof(client_list));

    head->Next=NULL;
    head->Client1=NULL;
    head->Client2=NULL;
    return head;

}


c_list newnode(int fd,int port,char *ip)
{
    
    c_list new_node=calloc(1,sizeof(client_list));
    new_node->fd=fd;
    strcpy(new_node->ipv4,ip);
    new_node->port=port;
    new_node->Next=NULL;

    return new_node;
    
}


int add2list(c_list head,int fd,int port,char *ip)
{
    if(head==NULL)
    {
        printf("add to list faliled :head is NULL\n");
        return -1;
    }
    if(find4list(head,port,ip))
    {
        return 0;
    }
    c_list new_node=newnode(fd,port,ip);
    printf("IP[%s]port[%d] fd:[%d] online\n",ip,port,fd);
    new_node->Next=head->Next;
    head->Next=new_node;
    
    return 1;
}


c_list Del4list(c_list head,int port,char *ip)
{
    if(head==NULL||head->Next==NULL)
    {
        printf("delete for list faliled :head is NULL\n");
        return NULL;
    }
   
    c_list tmp=find4list(head,port,ip);


    printf("DEL:ip:%s  prot:%d  fd:%d  is in list\n",tmp->ipv4,tmp->port,tmp->fd);

   
    c_list pos;
    for(pos=head;pos->Next!=tmp;pos=pos->Next);
 
    pos->Next=tmp->Next;
    tmp->Next=NULL;
    return tmp;
    

    
}

c_list find4list(c_list head,int port,char *ip)

{
        if(head==NULL||head->Next==NULL)
    {
        printf("find4list faliled :head is NULL\n");
        return NULL;
    }
    c_list tmp;
    for(tmp=head;tmp!=NULL;tmp=tmp->Next)
    {
        // printf("strcmp:%d\n",strcmp(tmp->ipv4,ip));
        if(port==tmp->port&&!strcmp(tmp->ipv4,ip))
        {
            return tmp;
        }

    }

     return NULL;
}

int show4list(c_list head)
{
     if(head==NULL||head->Next==NULL)
    {
        printf("show for list faliled :list is NULL\n");
       return -1;
    }
   
    for(c_list tmp=head->Next;tmp!=NULL;tmp=tmp->Next)
    {
        printf("ip:%s  prot:%d  fd:%d  state:online\n",tmp->ipv4,tmp->port,tmp->fd);
    }

    return 0;
}

int add2_Chatinglist(c_list head_connected,c_list head_chating,
                            int srcport,char *srcip,int tagport,char *tagip)
{
     
     if(head_connected==NULL||head_connected->Next==NULL||head_chating==NULL)
    {
        printf("add to chating list faliled :list is NULL\n");
       return -1;
    }

 
    if(find4_chatinglist(head_chating,srcport,srcip,tagport,tagip))
    {
        printf("Already exist in chating list\n");
        return 0;
    }
 
    c_list new_node=list_init();
 
    c_list src_node=find4list(head_connected,srcport,srcip);
    c_list tag_node=find4list(head_connected,tagport,tagip);
    if(src_node==NULL&&tag_node==NULL)
    {
        printf("error : unlegal client\n");
        return -1;
    }
 
    new_node->Client1=src_node;
    new_node->Client2=tag_node;
 
    new_node->Next=head_chating->Next;
    head_chating->Next=new_node;
 
    return 1;


}


c_list find4_chatinglist(c_list head_chating,int srcport,
                                            char *srcip,int tagport,char *tagip)

{    
        if(head_chating==NULL)
    {
        printf("find4_chatinglist faliled \n");
        return NULL;
    }
    if(tagport==0||tagip==NULL)
    {
        c_list pos;
        for(pos=head_chating->Next;pos!=NULL;pos=pos->Next)
        {
            if(pos->Client1->port==srcport&&
                            !strcmp(pos->Client1->ipv4,srcip)||pos->Client2->port==srcport
                        &&!strcmp(pos->Client2->ipv4,srcip))
            {
                 printf("find4_chatinglist faliled \n");
                return pos;
            }
        }
        return NULL;

    }
     
    c_list tmp;
    for(tmp=head_chating->Next;tmp!=NULL;tmp=tmp->Next)
    {
         
        // printf("strcmp:%d\n",strcmp(tmp->ipv4,ip));
        if(srcport==tmp->Client1->port&&!strcmp(tmp->Client1->ipv4,srcip)
                    &&tagport==tmp->Client2->port&&!strcmp(tmp->Client2->ipv4,tagip))
        {
             
            return tmp;
        }

    }

     return NULL;
}


c_list Del4_Chatinglist(c_list head_chating,int srcport,char *srcip)
{
      if(head_chating==NULL)
    {
        printf("Delete for chatinglist faliled \n");
        return NULL;
    }

   c_list tmp= find4_chatinglist(head_chating,srcport,srcip,0,NULL);

    if(NULL!=tmp)
    {
    c_list pos;
    for(pos=head_chating;pos->Next!=tmp;pos=pos->Next);

    pos->Next=tmp->Next;
    tmp->Next=NULL;
    return tmp;

    }

    return NULL;


}


int show4_chatinglist(c_list head_chating)
{
     if(head_chating==NULL)
    {
        printf("show chating list faliled ");
       return -1;
    }
   
    for(c_list tmp=head_chating->Next;tmp!=NULL;tmp=tmp->Next)
    {
        printf("ip:[%s] prot:[%d] is chating with ip:[%s] prot:[%d] \n",
                                         tmp->Client1->ipv4,tmp->Client1->port,
                                                tmp->Client2->ipv4,tmp->Client2->port);
    }

    return 0;
    
}


c_list new_groupnode(int groupnum)
{
    
    c_list new_node=calloc(1,sizeof(client_list));
    new_node->fd=groupnum;
    new_node->Client1=NULL;
    new_node->Next=NULL;

    return new_node;
    
}

c_list find4_grouplist(c_list head_group,int groupnum)
{

    if(head_group==NULL)
    {
        printf("find4_grouplist faliled \n");
        return NULL;
    }

    c_list tmp;
    for(tmp=head_group;tmp!=NULL;tmp=tmp->Next)
    {
        // printf("strcmp:%d\n",strcmp(tmp->ipv4,ip));
        if(groupnum==tmp->fd)
        {
            return tmp;
        }

    }

    return NULL;
}

int add2_grouplist(c_list head_group,int groupnum)
{
   
    if(head_group==NULL)
    {
        printf("add to group list faliled :head is NULL\n");
        return -1;
    }


   c_list new_node=new_groupnode(groupnum);
    new_node->Next=head_group->Next;
    head_group->Next=new_node;
   
    return 1;

}

c_list find4_groupmember(c_list head_member,int memberport,char *memberip)
{

    if(head_member==NULL)
    {
        printf("find4_groupmember faliled \n");
        return NULL;
    }

    c_list tmp;
    for(tmp=head_member;tmp!=NULL;tmp=tmp->Client1)
    {
        // printf("strcmp:%d\n",strcmp(tmp->ipv4,ip));
        if(memberport==tmp->port&&!strcmp(memberip,tmp->ipv4))
        {
            return tmp;
        }

    }

    return NULL;
}

int add2_groupmember(c_list head_member,int memberport,char *memberip)
{
     if(head_member==NULL)
    {
        printf("add2_groupmember faliled \n");
        return -1;
    }

    c_list new_node=newnode(0,memberport,memberip);
    printf("--%s--%d\n",__FUNCTION__,__LINE__);
    new_node->Client1=head_member->Client1;
    head_member->Client1=new_node;

    return 1;

}


int showgroup(c_list head_group)
{
     if(head_group==NULL||head_group->Next==NULL)
    {
        printf("show for group faliled :list is NULL\n");
       return -1;
    }
   
    for(c_list tmp=head_group->Next;tmp!=NULL;tmp=tmp->Next)
    {
        for(c_list pos=tmp->Client1;pos!=NULL;pos=pos->Client1)
       {
           printf("group[%d] member: IP[%s]---PORT[%d]\n",tmp->fd,pos->ipv4,pos->port);
       }
    }

    return 0;
}

int Del4_grouplist(c_list head_group,int memberport,char *memberip)
{
     if(head_group==NULL||head_group->Next==NULL)
    {
        printf("show for group faliled :list is NULL\n");
       return -1;
    }

     for(c_list tmp=head_group->Next;tmp!=NULL;tmp=tmp->Next)
    {
        for(c_list pos=tmp->Client1;pos!=NULL;pos=pos->Client1)
       {
          if(pos->port==memberport&&!strcmp(pos->ipv4,memberip))
          {
            c_list tem=pos;
            c_list ter;
            for(ter=tmp->Client1;ter->Client1!=tem;ter=ter->Client1);

            ter->Client1=tem->Client1;

            tem->Client1=NULL;
            free(tem);
          }
       }
    }
    return 0;
   
}